from datetime import datetime

import streamlit as st

@st.cache_resource
def get_resource():
    file = open("data_file.txt","a+")
    return file

file_handler = get_resource()

if st.button("Write to file"):
    ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    file_handler.write(f"{ts} - Hello World!\n")
    file_handler.flush()
    st.success("Written to file.")

if st.button("Read from file"):
    file_handler.seek(0)
    lines = file_handler.readlines()
    st.write(lines)
